package com.avic.rong.course.activity;

import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.avic.rong.BaseActivity;
import com.avic.rong.R;
import com.avic.rong.activity.login.LoginPhoneActivity;
import com.avic.rong.activity.product.OnlineCServiceActivity;
import com.avic.rong.course.YueView;
import com.avic.rong.course.adapter.PeiXunFindExpertRvAdapter;
import com.avic.rong.course.bean.FindExpertsBean;
import com.avic.rong.course.showapi.FindExpertApi;
import com.avic.rong.course.showapi.ToggleFollowApi;
import com.avic.rong.utils.ConstantsUtils;
import com.avic.rong.utils.DensityUtils;
import com.avic.rong.utils.SharedPreferencesUtil;
import com.avic.rong.utils.ToastTools;
import com.avic.rong.wedgit.loadingLayout.LoadingLayout;
import com.example.minato.minastore.BaseResultEntity;
import com.example.minato.minastore.http.HttpManager;
import com.example.minato.minastore.listener.HttpOnNextListener;
import com.orhanobut.logger.Logger;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.net.URLEncoder;

import butterknife.BindView;
import butterknife.ButterKnife;

public class PeiXunFindExpertActivity extends BaseActivity implements PeiXunFindExpertRvAdapter.FindExpertListener, OnRefreshListener, OnLoadMoreListener {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_right)
    ImageView ivRight;
    @BindView(R.id.ll_root)
    LinearLayout llRoot;
    @BindView(R.id.peixun_findexpert_rv)
    RecyclerView peixun_findexpert_rv;
    @BindView(R.id.loading_layout)
    LoadingLayout loading_layout;

    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    private PeiXunFindExpertRvAdapter findExpertRvAdapter;


    private FindExpertApi findExpertApi;
    private YueView yueView;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_pei_xun_find_expert;
    }

    @Override
    protected void initView() {

        ButterKnife.bind(this);
        tvTitle.setText("专家");
        ivRight.setImageResource(R.mipmap.gouwuche_dot);
        yueView = new YueView(this);

        yueView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (SharedPreferencesUtil.isLogin()) {
                    startActivity(OnlineCServiceActivity.getIntent(PeiXunFindExpertActivity.this, "ddd"));
                }else {
                    startActivity(LoginPhoneActivity.getIntent(context,"123456"));
                }
            }
        });
        yueView.show();
        peixun_findexpert_rv.setLayoutManager(new LinearLayoutManager(this));
        findExpertRvAdapter = new PeiXunFindExpertRvAdapter(this);
        findExpertRvAdapter.setListener(this);
        peixun_findexpert_rv.setAdapter(findExpertRvAdapter);
        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setOnLoadMoreListener(this);
        initData();
    }


    private void initData() {
        try {
            findExpertApi = new FindExpertApi(this, findExpertsListener);
            Logger.i("-----dd------" + SharedPreferencesUtil.getLoginToken());
            findExpertApi.setArgument(URLEncoder.encode(SharedPreferencesUtil.getLoginToken(), "UTF-8") , currentPageNumber, pageSize);
            HttpManager.getInstance().doHttpDeal(findExpertApi);
        }catch (Exception e){

        }
    }

    HttpOnNextListener<BaseResultEntity> findExpertsListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity baseResultEntity) {
            if (!baseResultEntity.getCode().equals(ConstantsUtils.STATUS_SUCCESS_0000)) {
                ToastTools.showToast(context, baseResultEntity.getMsg());
                return;
            }

            FindExpertsBean result = (FindExpertsBean) baseResultEntity.getResult();

            if (isRefresh) {
                findExpertRvAdapter.getDataList().clear();
            }
            findExpertRvAdapter.setDataList(result.getFollowList());

            if (findExpertRvAdapter.getDataList() == null || findExpertRvAdapter.getDataList().size() <= 0) {
                loading_layout.setStatus(LoadingLayout.EMPTY);
            } else {
                loading_layout.setStatus(LoadingLayout.SUCCESS);
            }

            if (isRefresh) {
                refreshLayout.finishRefresh();
            } else {
                refreshLayout.finishLoadMore();
            }

        }


        @Override
        public void onError(Throwable e) {
            super.onError(e);
            if (isRefresh) {
                refreshLayout.finishRefresh();
            } else {
                refreshLayout.finishLoadMore();
            }
            loading_layout.setStatus(LoadingLayout.ERROR);
            Logger.e("cjx", "培训列表数据：" + e.getMessage());

        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        yueView.destory();
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        isRefresh = true;
        currentPageNumber = 1;
        findExpertApi.setPageNo(currentPageNumber);
        HttpManager.getInstance().doHttpDeal(findExpertApi);
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        isRefresh = false;
        ++currentPageNumber;
        findExpertApi.setPageNo(currentPageNumber);
        HttpManager.getInstance().doHttpDeal(findExpertApi);
    }


    private PeiXunFindExpertRvAdapter.FindExpertViewHolder holder;
    private FindExpertsBean.FollowListBean bean;
    @Override
    public void setGuanZhu(PeiXunFindExpertRvAdapter.FindExpertViewHolder holder, FindExpertsBean.FollowListBean bean, int position) {
        this.holder = holder;
        this.bean = bean;
        ToggleFollowApi findExpertApi = new ToggleFollowApi((PeiXunFindExpertActivity) context, setGuanZhuListener, bean.getUserId(), SharedPreferencesUtil.getLoginToken());
        findExpertApi.setCache(false);
        HttpManager.getInstance().doHttpDeal(findExpertApi);
    }



    HttpOnNextListener<BaseResultEntity> setGuanZhuListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity baseResultEntity) {
            if (!baseResultEntity.getCode().equals(ConstantsUtils.STATUS_SUCCESS_200)) {
                ToastTools.showToast(context,baseResultEntity.getMsg());
                return;
            }
            bean.setIsFollow(bean.getIsFollow() == 0 ? 1 : 0);
            if (bean.getIsFollow() == 0) {
                holder.tvFollow.setText("关注");
                holder.tvFollow.setGravity(Gravity.CENTER_VERTICAL|Gravity.RIGHT);
                holder.tvFollow.setDrawablePaddingLeft(DensityUtils.dip2px(context,13));
                holder.tvFollow.setPadding(holder.tvFollow.getPaddingLeft(),holder.tvFollow.getPaddingTop(), DensityUtils.sp2px(context,13),holder.tvFollow.getPaddingBottom());
                holder.tvFollow.setSelected(false);
                holder.tvFollow.setTextColor(ContextCompat.getColor(context,R.color.white));
            } else {
                holder.tvFollow.setText("已关注");
                holder.tvFollow.setGravity(Gravity.CENTER);
                holder.tvFollow.setPadding(0,0,0,0);
                holder.tvFollow.setSelected(true);
                holder.tvFollow.setTextColor(ContextCompat.getColor(context,R.color.color_98c727));
            }
        }

        @Override
        public void onError(Throwable e) {
            super.onError(e);

        }
    };





    @Override
    protected void addListener() {

    }
}
